package com.jinpeng.huobikline.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.jinpeng.huobikline.common.JsonUtils;
import com.jinpeng.huobikline.common.RedisUtils;
import com.jinpeng.huobikline.common.ResDTO;
import com.jinpeng.huobikline.entity.ConfigVo;
import com.jinpeng.huobikline.entity.DataDTO;
import com.jinpeng.huobikline.entity.HuoBiDto;
import com.jinpeng.huobikline.entity.SymbolInfo;
import com.jinpeng.huobikline.service.KlineServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class KlineServicesImpl implements KlineServices {

    @Value("${huobi.1min}")
    private  String min1;

    @Value("${huobi.5min}")
    private  String min5;

    @Value("${huobi.15min}")
    private  String min15;

    @Value("${huobi.30min}")
    private  String min30;

    @Value("${huobi.60min}")
    private  String min60;

    @Value("${huobi.4hour}")
    private  String min4hour;

    @Value("${huobi.1day}")
    private  String oneDay;

    @Value("${huobi.1mon}")
    private  String onemon;

    @Value("${huobi.1week}")
    private  String oneweek;

    @Value("${huobi.1year}")
    private  String oneyear;

    @Value("${game.getwinningnumbers}")
    private  String getwinningnumbers;
    @Value("${game.updateKline}")
    private  String updateKline;
    @Resource
    private RedisUtils redisUtils;
    @Override
    public HuoBiDto oneKMinLineScheduled(String symbol,Integer number) {
        //链式构建请求
       String result2 = HttpRequest.post(String.format(min1,number>1?number:1,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:1:kline:endtime", String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:1:kline",map);
        return huoBiDto;
    }

    @Override
    public HuoBiDto fivesMinKLineScheduled(String symbol,Integer number) {
        //链式构建请求
        String result2 = HttpRequest.post(String.format(min5,number,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:5:kline:endtime", String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:5:kline",map);
        return huoBiDto;
    }

    @Override
    public HuoBiDto fifteenMinKLineScheduled(String symbol,Integer number) {
        //链式构建请求
        String result2 = HttpRequest.post(String.format(min15,number,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:15:kline:endtime", String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:15:kline",map);
        return huoBiDto;
    }

    @Override
    public HuoBiDto thirtyMinKLineScheduled(String symbol,Integer number) {
        //链式构建请求
        String result2 = HttpRequest.post(String.format(min30,number,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:30:kline:endtime", String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:30:kline",map);
        return huoBiDto;
    }

    @Override
    public HuoBiDto oneHourScheduled(String symbol,Integer number) {
        //链式构建请求
        String result2 = HttpRequest.post(String.format(min60,number,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:60:kline:endtime",String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:60:kline",map);
        return huoBiDto;
    }

    @Override
    public HuoBiDto fourHourScheduled(String symbol,Integer number) {
        //链式构建请求
        String result2 = HttpRequest.post(String.format(min4hour,number,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:4hour:kline:endtime", String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:4hour:kline",map);
        return huoBiDto;
    }

    @Override
    public HuoBiDto oneDayScheduled(String symbol,Integer number) {
        //链式构建请求
        String result2 = HttpRequest.post(String.format(oneDay,number,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:oneDay:kline:endtime", String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:oneDay:kline",map);
        return huoBiDto;
    }

    @Override
    public HuoBiDto oneMonScheduled(String symbol,Integer number) {
        //链式构建请求
        String result2 = HttpRequest.post(String.format(onemon,number,symbol))
                .timeout(20000)//超时，毫秒
                .execute().body();
        HuoBiDto huoBiDto = JsonUtils.toBean(result2,HuoBiDto.class);
        Map map = new HashMap();
        for (DataDTO data: huoBiDto.getData()) {
            map.put(String.valueOf(data.getId()),JsonUtils.toString(data));
        }
        redisUtils.set("btcusdt:onemon:kline:endtime", String.valueOf(System.currentTimeMillis()));
        redisUtils.hmset("btcusdt:onemon:kline",map);
        return huoBiDto;
    }

    @Override
    public void initKline(String symbol) {
        oneKMinLineScheduled(symbol,2000);
        fivesMinKLineScheduled(symbol,2000);
        fifteenMinKLineScheduled(symbol,2000);
        thirtyMinKLineScheduled(symbol,2000);
        oneHourScheduled(symbol,2000);
        fourHourScheduled(symbol,2000);
        oneDayScheduled(symbol,2000);
        oneMonScheduled(symbol,100);
    }

    @Override
    public Object symbolConfig() {
        ConfigVo configVo = new ConfigVo();
        List<String> exchange = new ArrayList<>();
        exchange.add("BTC_USDT");
        List<String> supportedResolutions = new ArrayList<>();
        supportedResolutions.add("5");
        configVo.setExchanges(exchange);
        configVo.setSupportsSearch(false);
        configVo.setSupportsGroupRequest(true);
        configVo.setSupportsMarks(false);
        configVo.setSupportsTime(false);
        configVo.setSupportedResolutions(supportedResolutions);
        return configVo;
    }

    @Override
    public Object symbolInfo(String group) {
        SymbolInfo symbolInfo = new SymbolInfo();
        List<String> type = new ArrayList<>();
        type.add("bitcoin");
        symbolInfo.setType(type);
        List<String> description = new ArrayList<>();
        description.add("BTC_USDT");
        symbolInfo.setDescription(description);
        symbolInfo.setHasdwm(true);
        symbolInfo.setHasintraday(true);
        List<Boolean> hasnovolume = new ArrayList<>();
        hasnovolume.add(false);
        symbolInfo.setHasnovolume(hasnovolume);
        symbolInfo.setMinmovement(1);
        symbolInfo.setMinmovement2(0);
        symbolInfo.setPricescale(1000000);
        symbolInfo.setSessionregular("24x7");
        List<String> symbol = new ArrayList<>();
        symbol.add("BTC_USDT");
        symbolInfo.setSymbol(symbol);
        List<String> ticker = new ArrayList<>();
        ticker.add("BTC_USDT");
        symbolInfo.setTicker(ticker);
        symbolInfo.setTimezone("Asia/Shanghai");
        return symbolInfo;
    }

    @Override
    public HuoBiDto getPrice() {
        HuoBiDto huoBiDto = fivesMinKLineScheduled("btcusdt", 1);
        return huoBiDto;
    }
}
